// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   AiccCourseImportAction.java

package com.iphaeton.lms.action.course.aicc;

import com.iphaeton.common.helper.IUploadHelper;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.course.scorm12.ICourseService;
import com.iphaeton.lms.business.course.scorm12.IItemService;
import com.iphaeton.lms.pojo.course.Course;
import com.iphaeton.lms.pojo.course.Item;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

// Referenced classes of package com.iphaeton.lms.action.course.aicc:
//            CourseBizSupportAction

public class AiccCourseImportAction extends CourseBizSupportAction
{

    public AiccCourseImportAction()
    {
    }

    public ActionForward importAICCZR(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String organize_id = "";
        String course_id = "";
        String uploadDir = "";
        String theWebPath = request.getRealPath("/");
        String courseName = (String)daf.get("courseName");
        FormFile courseFile = (FormFile)daf.get("courseFile");
        String controlType = (String)daf.get("controlType");
        String sort = (String)daf.get("sort");
        String criterion = (String)daf.get("criterion");
        String comment = (String)daf.get("comment");
        Integer hour = (Integer)daf.get("period");
        String fileName = courseFile.getFileName();
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String createrId = userSession.getUserId();
            String sessionID = request.getSession().getId();
            uploadDir = File.separatorChar + "tempUploadDir" + File.separatorChar + sessionID;
            logger.info("\u8BFE\u4EF6\u89E3\u538B\u7F29\u8DEF\u5F84:" + uploadDir);
            fileName = uploadDir + File.separatorChar + fileName;
            logger.info("\u8BFE\u4EF6\u6587\u4EF6\u8DEF\u5F84:" + fileName);
            organize_id = userSession.getOrgId();
            Course c = new Course();
            c.setSortId(sort);
            c.setName(courseName);
            c.setCreater(createrId);
            c.setComment(comment);
            c.setControl(controlType);
            c.setCriterion(criterion);
            c.setHour(hour);
            c.setState("a");
            c.setOrganizeId(organize_id);
            c = course.persistent(c);
            course_id = c.getId();
            Item i = new Item();
            i.setCourseId(course_id);
            i.setTitle(courseName);
            i.setType("sco");
            i.setLaunch("index.htm");
            i.setSequence(new Integer(0));
            i.setLevel(new Integer(1));
            i.setSn("01");
            item.save(i);
            upload.upload(courseFile, uploadDir + File.separatorChar);
            ZipFile archive = new ZipFile(fileName);
            byte buffer[] = new byte[16384];
            for(Enumeration e = archive.entries(); e.hasMoreElements();)
            {
                ZipEntry entry = (ZipEntry)e.nextElement();
                if(!entry.isDirectory())
                {
                    String filename = entry.getName();
                    filename = filename.replace('/', File.separatorChar);
                    filename = theWebPath + "course" + File.separator + course_id + File.separator + filename;
                    File destFile = new File(filename);
                    String parent = destFile.getParent();
                    if(parent != null)
                    {
                        File parentFile = new File(parent);
                        if(!parentFile.exists())
                            parentFile.mkdirs();
                    }
                    InputStream in = archive.getInputStream(entry);
                    OutputStream outStream = new FileOutputStream(filename);
                    int count;
                    while((count = in.read(buffer)) != -1) 
                        outStream.write(buffer, 0, count);
                    in.close();
                    outStream.close();
                }
            }

        }
        catch(BusinessException e1)
        {
            e1.printStackTrace();
            request.setAttribute("exception", e1);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("cid", course_id);
        request.setAttribute("oid", organize_id);
        return mapping.findForward("privateCourse");
    }

    private static Logger logger;
    private DynaActionForm daf;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.course.aicc.AiccCourseImportAction.class.getName());
    }
}
